package guyubao.com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guyubao.com.entity.dto.JDCertificateDto;
import guyubao.com.entity.vo.request.JDCertificateVoRequest;
import guyubao.com.entity.vo.response.JDCertificateVo;
import guyubao.com.mapper.JDCertificateMapper;
import guyubao.com.service.JDCertificateService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class JDCertificateServiceImpl extends ServiceImpl<JDCertificateMapper, JDCertificateDto> implements JDCertificateService {
    @Resource
    JDCertificateMapper jdCertificateMapper;
    /**
     * 添加
     */
    @Override
    public String updateJDCertificate(String uId, String pt_key, String pt_token, String pt_pin) {
        return this.update().set("pt_key",pt_key)
                .set("pt_token",pt_token)
                .set("pt_pin",pt_pin)
                .eq("uId",uId)
                .update() ? null : "数据库添加京东账号凭证失败！请联系管理人员!";
    }

    /**
     * 更新
     */
    @Override
    public String updateJDCertificate(JDCertificateVoRequest vo) {
        if (!this.update().set("uId",vo.getUid())
                .set("pt_key",vo.getPt_key())
                .set("pt_token",vo.getPt_token())
                .set("pt_pin",vo.getPt_pin())
                .eq("uId",vo.getUid())
                .update()) {
            return "数据库更新京东账号凭证失败！请联系管理人员！";
        }
        return null;
    }
    /***
     * 根据uid获取该账号 次数 状态
     */
    @Override
    public JDCertificateVo getJDCertificateProofBase(String uid) {
        return jdCertificateMapper.getCountByUId(uid);
    }
    /***
     * 根据uid获取该账号凭证信息
     */
    @Override
    public JDCertificateDto getJDCertificateProof(String uid) {
        return this.query().eq("uId",uid).one();
    }

    /***
     * 根据uid参试扣除一次账号
     */
    @Override
    public String selfAttenuation(String uid) {
        JDCertificateDto jdCertificateDto = this.query().eq("uId", uid).one();
        int count = jdCertificateDto.getCount();
        if (count <1) return "您的次数不足以更新凭证";
        return this.update().set("count",--count).eq("uId",uid).update() ? null : "减小次数失败，请联系管理人员！";
    }
    /**
     * 根据uid修改账号状态
     */
    @Override
    public String updateJDCertificateState(String uid) {
        return this.update().set("state",1).eq("uId",uid).update() ? null:"更新凭证成功！修改账户状态失败！请联系管理人员！";
    }

}
